<?php

/**
 * survey actions.
 *
 * @package    sf_sandbox
 * @subpackage survey
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class surveyActions extends sfActions {

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {

        if ($moodle_user_id = $this->getUser()->getUserMoodle()) {

            $courses = Doctrine::getTable("MdlCourse")->getCourseByUser($moodle_user_id, 5);

            if (count($courses)) {
                
                //Instancio conexion a la base de datos en MongoDB
                $ss_instance = MongoManager::getConention();
                //Obtengo una instancia de la coleccion
                $assignments = $ss_instance->getCollection("assignments");
                $surveys = $ss_instance->getCollection("surveys");
                //Almacen de encuestas asignidas
                $survey_assignments = array();
   
                foreach ($courses as $key => $course) {
 
                    //Consultar asignaciones del curso.
                    $course_assignment = $assignments->find(array('course_id' => (int)$course["id"]));
                    
                    if( !$course_assignment->count() )
                        continue;
                    
                    foreach($course_assignment as $_id => $assign){
                        
                        //Obtengo información de la encuesta asignada
                        $survey = $surveys->findOne(
                                array(
                                    "_id"=>new MongoId($assign["survey_id"])
                                ),array("name")
                        );
                        
                        //Guardo información de la encuesta 
                        $survey_assignments[] = array(
                          "assig_id"=>  $_id,
                          "survey_name"=> $survey["name"]
                        );
                    }
                    
                }
                
                //Cierro la conexion de la instancia
                $ss_instance->close(); 
                
                //PARA VISUALIZAR EN LA VISTA
                $this->survey_assignments = $survey_assignments;

            } else {
                //NO TIENE CURSOS
                return sfView::NONE;
            }
            
        }else {
           //NO ESTA LOGEADO
           return sfView::NONE;
        }
    }

    /**
     * Executes ViewSurvey action
     *Visualiza la encuesta para la asignación.
     * @param int $aid a Assignment ID
     * @param sfRequest $request A request object
     */
    public function executeViewSurvey(sfWebRequest $request) {

        if (!$moodle_user_id = $this->getUser()->getUserMoodle()) {
            //NO ESTA LOGEADO
            return sfView::NONE;
        }

        $assig_id = $request->getParameter("aid");

        if (!$assig_id) {
            //NO HAY REFERENCIA DE ENCUESTAS
            $this->redirect("survey/index");
        }

        //Instancio conexion a la base de datos en MongoDB
        $ss_instance = MongoManager::getConention();
        //Obtengo una instancia de la coleccion
        $assignments_collection = $ss_instance->getCollection("assignments");
        $surveys_collection = $ss_instance->getCollection("surveys");

        $course_assignment = $assignments_collection->findOne(array('_id' => new MongoID($assig_id)));
        
        if (!count($course_assignment)) {
            //NO EXISTE ASIGNACIÓN REFERENCIADA
            $this->redirect("survey/index");
        }
        
        $survey = $surveys_collection->findOne( array("_id"=>new MongoId($course_assignment["survey_id"]) ) );
        
        if(!count($survey)){
            //NO EXISTE ENCUESTA REFERENCIADA
            $this->redirect("survey/index");
        }
        $this->assignment = $course_assignment;
        $this->survey = $survey;
    }

    
    public function executeAddTypeQuestion(sfWebRequest $request){
        $type = $request->getParameter("type");
        $this->type = $type;
    }
    
}
